from typing import TYPE_CHECKING
import pygame
from pygame import draw
from const import (
    GRAY, BLACK, LIGHT_GRAY, WHITE,
    PLAYER_PANEL_WIDTH, STATUS_PANEL_HEIGHT,
    SCREEN_HEIGHT, SCREEN_WIDTH,
    RESTART_GAME_EVENT
)

if TYPE_CHECKING:
    from model import PlayerManager
    from .renderer import FontManager


class RestartButton:

    width: int
    height: int
    screen: pygame.Surface
    font: pygame.font.Font
    color: tuple[int, int, int]
    text_color: tuple[int, int, int]
    text: pygame.Surface

    def __init__(self, screen, font_mgr: 'FontManager',
        width:int=80, height:int=30,
        color:tuple[int, int, int]=LIGHT_GRAY, 
        text_color:tuple[int, int, int]=BLACK):

        self.screen = screen
        self.font = font_mgr.get_font(12)
        self.width = width
        self.height = height
        self.color = color
        self.text_color = text_color
        self.text = self.font.render("Restart", True, text_color)

        self.rect = pygame.Rect(0, 0, width, height)
        
    def draw(self, x: int, y: int):

        self.x = x
        self.y = y

        # 绘制圆角矩形背景
        draw.rect(
            self.screen,
            self.color,
            (x, y, self.width, self.height),
            border_radius=5
        )
        
        self.rect = pygame.Rect(x, y, self.width, self.height)
        draw.rect(
            self.screen,
            BLACK,
            self.rect,
            width=1,
            border_radius=5
        )
        # 居中绘制文字
        text_rect = self.text.get_rect(center=(x+40, y+15))
        self.screen.blit(self.text, text_rect)

    def is_mouse_over(self, mouse_pos: tuple[int, int]) -> bool:
        mx, my = mouse_pos
        return self.rect.collidepoint(mx, my)

    def check_hover(self, mouse_pos: tuple[int, int]) -> bool:
        """检查鼠标是否悬停在按钮上"""
        if self.rect.collidepoint(mouse_pos):
            self.color = WHITE
        else:
            self.color = LIGHT_GRAY

        
        
    def handle_click(self) -> None:
        """处理按钮点击事件，发送重启游戏事件"""
        pygame.event.post(pygame.event.Event(RESTART_GAME_EVENT))

class PlayerAreaRenderer:

    screen: pygame.Surface

    def __init__(self, screen, font_mgr: 'FontManager'):
        self.screen = screen
        self.font_mgr = font_mgr
        self.restart_button = RestartButton(screen, font_mgr)


    def draw_top_area(self, player_manager: 'PlayerManager') -> None:
        
        small_font = self.font_mgr.get_font(16)

        y_pos = 10
        # 当前玩家文字
        current_text = small_font.render("Current Player:", True, BLACK)        
        self.screen.blit(current_text, (20, y_pos))
        current_player_text = small_font.render(player_manager.current_player.name, True, BLACK)
        
        y_pos = current_text.get_height() + y_pos
        self.screen.blit(current_player_text, (20, y_pos))
        
        y_pos = current_text.get_height() + y_pos
        btn_x = PLAYER_PANEL_WIDTH / 2 - self.restart_button.width / 2
        btn_y = y_pos + 15
        self.restart_button.draw(btn_x, btn_y)


    def draw_bottom_area(self, player_manager: 'PlayerManager'):
        font = self.font_mgr.get_font(36)
                
        # 下部分：玩家列表（调整起始Y坐标）
        top_margin = 20

        for i, player in enumerate(player_manager.players):
            y_pos = STATUS_PANEL_HEIGHT + top_margin + i * 100
            
            name_text = font.render(player.name, True, player.color)

            # 先绘制当前玩家高亮背景（新增部分）
            if player == player_manager.current_player:
                draw.rect(
                    self.screen, 
                    WHITE,  # 白色背景
                    (10, y_pos - 5, PLAYER_PANEL_WIDTH - 20, name_text.get_height() + 20),  # 调整高度以适应文本
                    border_radius=10  # 圆角矩形
                )
            
            
            self.screen.blit(name_text, (20, y_pos))

    def draw_players(self, player_manager: 'PlayerManager') -> None:
        

        # 上部分：当前玩家信息面板
        # 绘制玩家区域背景
        draw.rect(self.screen, GRAY, (0, 0, PLAYER_PANEL_WIDTH, SCREEN_HEIGHT))
        # 绘制上下分割线
        draw.line(self.screen, BLACK, (0, STATUS_PANEL_HEIGHT), (PLAYER_PANEL_WIDTH, STATUS_PANEL_HEIGHT), 2)
        # 绘制左右分割线
        draw.line(self.screen, BLACK, (PLAYER_PANEL_WIDTH, 0), (PLAYER_PANEL_WIDTH, SCREEN_HEIGHT), 2)
        
        
        self.draw_top_area(player_manager)
        self.draw_bottom_area(player_manager)
    
    def handle_click(self, mouse_pos: tuple[int, int]) -> bool:
        """
        处理玩家区域的点击事件
        
        Args:
            mouse_pos: 鼠标点击位置坐标
            
        Returns:
            bool: 如果点击在玩家区域内返回True，否则返回False
        """
        # 判断点击是否在玩家区域内
        mx, my = mouse_pos
        if mx <= PLAYER_PANEL_WIDTH:
            # 检查是否点击了重启按钮
            if self.restart_button.is_mouse_over(mouse_pos):
                self.restart_button.handle_click()
            return True
        return False
    
    def handle_mouse_hover(self, mouse_pos: tuple[int, int]) -> None:
        """
        处理鼠标悬停事件
        """
        # 检查鼠标是否悬停在重启按钮上
        self.restart_button.check_hover(mouse_pos)
        # if self.restart_button.is_mouse_over(mouse_pos):
        #     self.restart_button.color = WHITE  
        # else:
        #     self.restart_button.color = LIGHT_GRAY




